package com.ruoyi.storage.service.impl;

import java.util.List;

import com.ruoyi.storage.mapper.ItemDetailMapper;
import com.ruoyi.storage.mapper.WarehouseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.storage.domain.ItemDetail;
import com.ruoyi.storage.mapper.WarehouseInventoryMapper;
import com.ruoyi.storage.domain.WarehouseInventory;
import com.ruoyi.storage.service.IWarehouseInventoryService;

/**
 * 库存查询Service业务层处理
 * 
 * @author 庞一恺
 * @date 2025-01-11
 */
@Service
public class WarehouseInventoryServiceImpl implements IWarehouseInventoryService 
{
    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;
    @Autowired
    private WarehouseInfoMapper warehouseInfoMapper;
    @Autowired
    private ItemDetailMapper itemDetailMapper;

    /**
     * 查询库存查询
     * 
     * @param batchId 库存查询主键
     * @return 库存查询
     */
    @Override
    public WarehouseInventory selectWarehouseInventoryByBatchId(String batchId)
    {
        return warehouseInventoryMapper.selectWarehouseInventoryByBatchId(batchId);
    }

    /**
     * 查询库存查询列表
     *
     * @param warehouseInventory 库存查询
     * @return 库存查询
     */
    @Override
    public List<WarehouseInventory> selectWarehouseInventoryList(WarehouseInventory warehouseInventory, Long userId)
    {
        Integer warehouseId = warehouseInfoMapper.getWarehouseIdByPrincipal(Math.toIntExact(userId));
        if (warehouseId == null) return new ArrayList<WarehouseInventory>();
        warehouseInventory.setWarehouseId(warehouseId);
        return warehouseInventoryMapper.selectWarehouseInventoryList(warehouseInventory);
    }

    /**
     * 新增库存查询
     * 
     * @param warehouseInventory 库存查询
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWarehouseInventory(WarehouseInventory warehouseInventory)
    {
        int rows = warehouseInventoryMapper.insertWarehouseInventory(warehouseInventory);
        insertItemDetail(warehouseInventory);
        return rows;
    }

    /**
     * 修改库存查询
     * 
     * @param warehouseInventory 库存查询
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWarehouseInventory(WarehouseInventory warehouseInventory)
    {
        warehouseInventoryMapper.deleteItemDetailByBatchId(warehouseInventory.getBatchId());
        insertItemDetail(warehouseInventory);
        return warehouseInventoryMapper.updateWarehouseInventory(warehouseInventory);
    }

    /**
     * 批量删除库存查询
     * 
     * @param batchIds 需要删除的库存查询主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWarehouseInventoryByBatchIds(String[] batchIds)
    {
        warehouseInventoryMapper.deleteItemDetailByBatchIds(batchIds);
        return warehouseInventoryMapper.deleteWarehouseInventoryByBatchIds(batchIds);
    }

    /**
     * 删除库存查询信息
     * 
     * @param batchId 库存查询主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWarehouseInventoryByBatchId(String batchId)
    {
        warehouseInventoryMapper.deleteItemDetailByBatchId(batchId);
        return warehouseInventoryMapper.deleteWarehouseInventoryByBatchId(batchId);
    }

    @Override
    public List<ItemDetail> selectItemDetailByBatchId(String batchId) {
        ItemDetail itemDetail = new ItemDetail();
        itemDetail.setBatchId(batchId);
        return itemDetailMapper.selectItemDetailList(itemDetail);
    }

    /**
     * 货品配送入库更新状态
     *
     * @param warehouseInventory 库存查询主键
     * @return 结果
     */
    @Override
    public int updateWarehouseInventoryDeliveryEntry(WarehouseInventory warehouseInventory) {


        return warehouseInventoryMapper.updateWarehouseInventoryDeliveryEntry(warehouseInventory);
    }

    /**
     * 新增库存详情信息
     * 
     * @param warehouseInventory 库存查询对象
     */
    public void insertItemDetail(WarehouseInventory warehouseInventory)
    {
        List<ItemDetail> itemDetailList = warehouseInventory.getItemDetailList();
        String batchId = warehouseInventory.getBatchId();
        if (StringUtils.isNotNull(itemDetailList))
        {
            List<ItemDetail> list = new ArrayList<ItemDetail>();
            for (ItemDetail itemDetail : itemDetailList)
            {
                itemDetail.setBatchId(batchId);
                list.add(itemDetail);
            }
            if (list.size() > 0)
            {
                warehouseInventoryMapper.batchItemDetail(list);
            }
        }
    }
}
